//
//  ShinnyHollywoodGalleryDetailImageViewer.m
//  ShinnyHollywoodGalleryController
//
//  Created by Dario Lencina on 6/28/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ShinnyHollywoodGalleryDetailImageViewer.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/QuartzCore.h>
#import "UIImageExtras.h"

@implementation ShinnyHollywoodGalleryDetailImageViewer
@synthesize backroundImage;
@synthesize frontImage, delegate;

#define kwidthOfDetailImageContainer 264
#define kheightOfDetailImageContainer 224
#define kscaleOfDetailImageContainer 1
#define kSizeOfCloseButton 50

-(id)initWithFrame:(CGRect)frame{
	backroundImage= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MetalBackround.png"]];
	self= [super initWithFrame: backroundImage.frame];
	if (self) {
		[self addSubview:backroundImage];
		backroundImage.center=CGPointMake(self.center.x, self.center.y) ;
		[frontImage release];
		_closeButton= [UIButton buttonWithType:UIButtonTypeCustom];
		_hiddenImage=nil;
		_closeButton.frame = CGRectMake(self.frame.size.width-kSizeOfCloseButton,0, kSizeOfCloseButton, kSizeOfCloseButton);
		[_closeButton setImage:[UIImage imageNamed:@"closeButton.png"] forState:UIControlStateNormal];
		[_closeButton addTarget:self 
				   action:@selector(buttonClicked:) 
		 forControlEvents:UIControlEventTouchUpInside];
		[_closeButton retain];
		[self addSubview:_closeButton];
		[self setOpaque:TRUE];
	}
	return self;
}

-(void)buttonClicked:(id)button{
	if (delegate) 
		if([delegate conformsToProtocol:@protocol(ShinnyHollywoodGalleryDetailImageViewerDelegate)])
			[delegate closeButtonPressed:button inShinnyHollywoodGalleryDetailImageViewer:self];
}


-(void)pushImage:(UIImage *)anImage from:(NSString *)transitionSideToPushFrom{
	CATransition * loginTransition = nil;
	if(frontImage){
		if(_hiddenImage==nil){
			_hiddenImage= [[UIImageView alloc] initWithImage:frontImage.image];
			[self addSubview:_hiddenImage];
		}
		else 
			[_hiddenImage setFrame:frontImage.frame];
		
		[_hiddenImage setImage:frontImage.image];					
		
		[frontImage removeFromSuperview];
	}
	loginTransition = [CATransition animation];
	loginTransition.duration = [[UIApplication sharedApplication] statusBarOrientationAnimationDuration];
	loginTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
	loginTransition.type = kCATransitionPush;
	loginTransition.subtype = transitionSideToPushFrom;

	frontImage= [[UIImageView alloc] initWithImage: [anImage imageByScalingAndCroppingForSize:CGSizeMake(220 * kscaleOfDetailImageContainer, 168 * kscaleOfDetailImageContainer)]];
	frontImage.center= CGPointMake(backroundImage.center.x+6, backroundImage.center.y-2);	
	_hiddenImage.center= frontImage.center;
	[self addSubview:frontImage];
	[self bringSubviewToFront: _closeButton];
	[frontImage release];
	
	[loginTransition setRemovedOnCompletion:TRUE];
	[[frontImage layer] addAnimation:loginTransition forKey:@"hidding"];
}

-(void)setFrontImageProperty:(UIImage *)anImage animated:(BOOL)animated{
	CATransition * loginTransition = nil;
	if(frontImage){
		if(_hiddenImage==nil){
			_hiddenImage= [[UIImageView alloc] initWithImage:frontImage.image];
			[self addSubview:_hiddenImage];
		}
		else 
			[_hiddenImage setFrame:frontImage.frame];
		
		[_hiddenImage setImage:frontImage.image];					

		[frontImage removeFromSuperview];
	}
	if(animated){
		loginTransition = [CATransition animation];
		loginTransition.duration = [[UIApplication sharedApplication] statusBarOrientationAnimationDuration];
		loginTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
		loginTransition.type = kCATransitionPush;
		loginTransition.subtype = kCATransitionFromTop;
	}
		frontImage= [[UIImageView alloc] initWithImage: [anImage imageByScalingAndCroppingForSize:CGSizeMake(220 * kscaleOfDetailImageContainer, 168 * kscaleOfDetailImageContainer)]];
		frontImage.center= CGPointMake(backroundImage.center.x+6, backroundImage.center.y-2);	
		_hiddenImage.center= frontImage.center;
		[self addSubview:frontImage];
		[self bringSubviewToFront: _closeButton];
		[frontImage release];
	
	if (animated) {
		[loginTransition setRemovedOnCompletion:TRUE];
		[[frontImage layer] addAnimation:loginTransition forKey:@"hidding"];
	}
}

-(void)setHidden:(BOOL)hidden{	
	NSString * transitionSubtype= nil;
	NSString * transitionName= nil;
	
	if (!hidden) {
		transitionSubtype= kCATransitionFromBottom;
		transitionName= @"hiddingDetailImageViewer";
	}else {
		transitionSubtype= kCATransitionFromTop;
		transitionName= @"showingDetailImageViewer";
	}
	CATransition * loginTransition=nil;
	loginTransition = [CATransition animation];
	loginTransition.duration = 	[[UIApplication sharedApplication] statusBarOrientationAnimationDuration];
	loginTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
	loginTransition.type = kCATransitionPush;
	loginTransition.subtype = transitionSubtype;
	loginTransition.delegate=self;
	[super setHidden:hidden];
	[[self layer] addAnimation:loginTransition forKey:transitionName];	
	[loginTransition setRemovedOnCompletion:TRUE];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
	if (delegate) 
		if([delegate conformsToProtocol:@protocol(ShinnyHollywoodGalleryDetailImageViewerDelegate)])
			[delegate animationEndedInShinnyHollywoodGalleryDetailImageViewer:self];
}

static CGFloat initialx=0;
#define kUbral 40;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch * atouch=[touches anyObject];
	CGPoint point= [atouch locationInView:self];
	initialx= point.x;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch * atouch=[touches anyObject];
	CGPoint point= [atouch locationInView:self];
	if(initialx - point.x >40){
		if (delegate) 
			if([delegate conformsToProtocol:@protocol(ShinnyHollywoodGalleryDetailImageViewerDelegate)])
				[delegate needsNextImageInShinnyHollywoodGalleryDetailImageViewer:self];
		return;
	}
	
	if(initialx - point.x <-40){
		if (delegate) 
			if([delegate conformsToProtocol:@protocol(ShinnyHollywoodGalleryDetailImageViewerDelegate)])
				[delegate needsPreviousImageInShinnyHollywoodGalleryDetailImageViewer:self];
	}
}
	


-(void)dealloc{	
	delegate=nil;
	
	if(backroundImage)
		[backroundImage release];
	if (frontImage)
		[frontImage release];
	if(_hiddenImage)
		[_hiddenImage release];
	
	[_closeButton release];
	
	[super dealloc];
}
@end
